import com.alibaba.excel.EasyExcel;

import java.io.File;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;


public class DormitoryManager implements Serializable {
    private String dbFilePath = "E:\\dormitory-demo\\src\\main\\resources\\student.db";
    private List<Dormitory> maleDormitories;
    private List<Dormitory> femaleDormitories;

    public String getDbFilePath() {
        return dbFilePath;
    }

    public void setDbFilePath(String dbFilePath) {
        this.dbFilePath = dbFilePath;
    }

    public List<Dormitory> getMaleDormitories() {
        return maleDormitories;
    }

    public void setMaleDormitories(List<Dormitory> maleDormitories) {
        this.maleDormitories = maleDormitories;
    }

    public List<Dormitory> getFemaleDormitories() {
        return femaleDormitories;
    }

    public void setFemaleDormitories(List<Dormitory> femaleDormitories) {
        this.femaleDormitories = femaleDormitories;
    }

    public void store() {
        try {
            File file = new File(dbFilePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(file.toPath()));
            oos.writeObject(this);
            oos.flush();
            oos.close();
            System.out.println("保存数据成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("保存数据失败");
        }

    }

    public boolean restore() {
        try {
            File file = new File(dbFilePath);
            if (!file.exists()) {
                return false;
            }
            ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(Paths.get(dbFilePath)));
            DormitoryManager dormitoryManager = (DormitoryManager) ois.readObject();
            if (null == dormitoryManager) {
                return false;
            }
            this.femaleDormitories = dormitoryManager.femaleDormitories;
            this.maleDormitories = dormitoryManager.maleDormitories;
            return true;
        } catch (Exception e) {
            System.out.println("加载数据异常");
        }

        return false;
    }


    public void importExcel(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            System.out.println("指定 Excel 不存在，请重试！");
            return;
        }
        try {
            EasyExcel.read(Files.newInputStream(file.toPath()))
                    .head(Student.class)
                    .registerReadListener(new ExcelImport<Student>(this)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("文件格式错误，请重试");
        }
    }

    public DormitoryManager(int maleDormitoryCount, int femaleDormitoryCount, int bedCount) {
        maleDormitories = new ArrayList<>();
        for (int i = 0; i < maleDormitoryCount; i++) {
            maleDormitories.add(new Dormitory(i + 1, bedCount));
        }

        femaleDormitories = new ArrayList<>();
        for (int i = 0; i < femaleDormitoryCount; i++) {
            femaleDormitories.add(new Dormitory(i + 1, bedCount));
        }
    }

    //展示可用床位信息
    public void displayAvailableBeds(String gender) {
        List<Dormitory> dormitories = (gender.equals("男") ? maleDormitories : femaleDormitories);
        System.out.println(gender + "生宿舍空床位：");
        for (Dormitory dormitory : dormitories) {
            System.out.println("宿舍【" + dormitory.getNumber() + "】");
            for (int i = 0; i < dormitory.getStudents().size(); i++) {
                if (dormitory.getStudents().get(i) == null) {
                    System.out.print((i + 1) + "床 ");
                }
            }
            System.out.println();
        }
    }

    //添加学生
    public void addStudent(String name, String gender, int dormitoryNumber, int bedNumber) {
        List<Dormitory> dormitories = (gender.equals("男") ? maleDormitories : femaleDormitories);

        Dormitory dormitory = findDormitoryByNumber(dormitoryNumber, dormitories);
        if (dormitory != null) {
            if (bedNumber >= 1 && bedNumber <= dormitory.getStudents().size()) {
                if (dormitory.getStudents().get(bedNumber - 1) == null) {
                    Student student = new Student(name, gender);
                    dormitory.getStudents().set(bedNumber - 1, student);
                    student.setDormitoryNumber(dormitory.getNumber());
                    student.setBedNumber(bedNumber);
                    System.out.println("分配成功，分配" + name + "到" + gender + "生宿舍" + dormitory.getNumber() + "的" + bedNumber + "号床");
                } else {
                    System.out.println("分配失败，分配" + name + "到" + gender + "生宿舍" + dormitory.getNumber() + "的" + bedNumber + "号床失败，该床位已被占用");
                }
            } else {
                System.out.println("无效的床位号");
            }
        } else {
            System.out.println("未找到该宿舍");
        }
    }

    //查询学生
    public void searchStudent(String gender, String name) {
        List<Dormitory> dormitories = (gender.equals("男") ? maleDormitories : femaleDormitories);
        for (Dormitory dormitory : dormitories) {
            for (int i = 0; i < dormitory.getStudents().size(); i++) {
                Student student = dormitory.getStudents().get(i);
                if (student != null && student.getName().equals(name)) {
                    System.out.println(name + "住在" + gender + "宿舍" + dormitory.getNumber() + "的" + (i + 1) + "号床");
                    return;
                }
            }
        }

        System.out.println("未找到该学生");
    }

    //给宿舍评分
    public void rateDormitory(String gender, int dormitoryNumber, int score) {
        List<Dormitory> dormitories = (gender.equals("男") ? maleDormitories : femaleDormitories);

        Dormitory dormitory = findDormitoryByNumber(dormitoryNumber, dormitories);
        if (dormitory != null) {
            dormitory.setCleanlinessScore(score);
            System.out.println("评分成功");
        } else {
            System.out.println("未找到该宿舍");
        }
    }

    //添加警告
    public void addWarning(String gender, int dormitoryNumber, String warning) {
        List<Dormitory> dormitories = (gender.equals("男") ? maleDormitories : femaleDormitories);
        Dormitory dormitory = findDormitoryByNumber(dormitoryNumber, dormitories);
        if (dormitory != null) {
            dormitory.addWarning(warning);
            System.out.println("警告添加成功");
        } else {
            System.out.println("未找到该宿舍");
        }
    }

    //展示宿舍整体情况，床位，学生，评分，警告
    public void displayDormitoryInfo(String gender, int dormitoryNumber) {
        List<Dormitory> dormitories = (gender.equals("男") ? maleDormitories : femaleDormitories);
        Dormitory dormitory = findDormitoryByNumber(dormitoryNumber, dormitories);
        if (dormitory != null) {
            displayDormitoryInfo(dormitory);
        } else {
            System.out.println("未找到该宿舍");
        }
    }

    private Dormitory findDormitoryByNumber(int number, List<Dormitory> dormitories) {
        for (Dormitory dormitory : dormitories) {
            if (dormitory.getNumber() == number) {
                return dormitory;
            }
        }
        return null;
    }

    private void displayDormitoryInfo(Dormitory dormitory) {
        System.out.println("宿舍" + dormitory.getNumber() + "的学生床位情况：");
        for (int i = 0; i < dormitory.getStudents().size(); i++) {
            Student student = dormitory.getStudents().get(i);
            if (student != null) {
                System.out.print((i + 1) + "号床：" + student.getName() + " | ");
            } else {
                System.out.print((i + 1) + "号床：空床 ｜ ");
            }
        }
        System.out.println();
        System.out.println("宿舍" + dormitory.getNumber() + "的评分情况：");
        System.out.println("得分：" + dormitory.getCleanlinessScore());
        System.out.println("宿舍" + dormitory.getNumber() + "的警告情况：");
        if (dormitory.getWarnings().size() == 0) {
            System.out.println("无");
        } else {
            for (String warning : dormitory.getWarnings()) {
                System.out.println("- " + warning);
            }
        }

    }
}
